package com.ruoyi.venue.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.field.mapper.FieldMapper;
import com.ruoyi.reservation.domain.Reservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.field.domain.Field;
import com.ruoyi.venue.mapper.VenueMapper;
import com.ruoyi.venue.domain.Venue;
import com.ruoyi.venue.service.IVenueService;

/**
 * 场馆Service业务层处理
 * 
 * @author HuLongHu
 * @date 2023-02-11
 */
@Service
public class VenueServiceImpl implements IVenueService 
{
    @Autowired
    private VenueMapper venueMapper;

    @Autowired
    private FieldMapper fieldMapper;
    /**
     * 查询场馆
     * 
     * @param venueId 场馆主键
     * @return 场馆
     */
    @Override
    public Venue selectVenueByVenueId(Long venueId)
    {
        return venueMapper.selectVenueByVenueId(venueId);
    }

    /**
     * 查询场馆列表
     * 
     * @param venue 场馆
     * @return 场馆
     */
    @Override
    public List<Venue> selectVenueList(Venue venue)
    {
        return venueMapper.selectVenueList(venue);
    }

    /**
     * 新增场馆
     * 
     * @param venue 场馆
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVenue(Venue venue)
    {
        venue.setCreateTime(DateUtils.getNowDate());
        int rows = venueMapper.insertVenue(venue);
        insertField(venue);
        return rows;
    }

    /**
     * 修改场馆
     * 
     * @param venue 场馆
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVenue(Venue venue)
    {
        venue.setUpdateTime(DateUtils.getNowDate());
        venueMapper.deleteFieldByVenueId(venue.getVenueId());
        insertField(venue);
        return venueMapper.updateVenue(venue);
    }

    /**
     * 批量删除场馆
     * 
     * @param venueIds 需要删除的场馆主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVenueByVenueIds(Long[] venueIds)
    {
        venueMapper.deleteFieldByVenueIds(venueIds);
        return venueMapper.deleteVenueByVenueIds(venueIds);
    }

    /**
     * 删除场馆信息
     * 
     * @param venueId 场馆主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVenueByVenueId(Long venueId)
    {
        venueMapper.deleteFieldByVenueId(venueId);
        return venueMapper.deleteVenueByVenueId(venueId);
    }

    /**
     * @param fieldName
     * @return
     */
    @Override
    public List<Field> venueReservationList(String fieldName) {
       return venueMapper.venueReservationList(fieldName);
    }



    /**
     * 新增场地信息
     * 
     * @param venue 场馆对象
     */
    public void insertField(Venue venue)
    {
        List<Field> fieldList = venue.getFieldList();
        Long venueId = venue.getVenueId();
        if (StringUtils.isNotNull(fieldList))
        {
            List<Field> list = new ArrayList<Field>();
            for (Field field : fieldList)
            {
                field.setVenueId(venueId);
                list.add(field);
            }
            if (list.size() > 0)
            {
                venueMapper.batchField(list);
            }
        }
    }
}
